#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re

import jieba

#jieba cut 返回touple
def strToWords(string):
    return list(jieba.cut(string))

#比较str1，str2，rate为比对的阈值（0-1），返回包含的字符
def compareString(str1,str2,rate):
    if rate <=0 or rate >1:
        print('======> rate 需要在0-1之间')
        raise Exception
    words1=strToWords(str1)
    i=0
    result=[]
    for word in words1:
        if str2.find(word)>=0:
            result.append(word)
            i+=1
            #达到阈值，返回
            if i/words1.__len__()>=rate:
                break
    return result

#比较str2包含的str1字符的比率>= rate
def compareStringRate(str1,str2,rate):
    result=[]
    words=strToWords(str1)
    if words is None:
        raise Exception('无效的words:str={}'.format(str1))
    if words.__len__() <= 0:
        return False

    result=compareString(str1,str2,rate)
    if result.__len__()/words.__len__()>=rate:
        return True
    else:
        return False

def removeNum(str1:str):
    pattern=r'^[a-zA-Z\_]*'
    result=re.match(pattern, str1)
    if result:
        return result.group()
    else:
        return False

def regmatch(str):
    r = r'(\w+)@(\w+).com'
    if re.match(r, str):
        return True
    else:
        return False


if __name__ == '__main__':
    # str1='四川省成都市武侯大道顺江段61号'
    # str2='四川省成都市武侯区武侯大道顺江段61号'
    # print(strToWords(str1))
    #
    # result=compareString(str1,str2,0.7)
    # print(result)
    #
    # str1='beijing234545435435435'
    # print(removeNum(str1))
    # assert regmatch('someone@gmail.com')
    # assert not regmatch('mr-bob@example.com')
    # assert regmatch('fsdf@fsdfs')

    str3= '半身裙女秋季高腰显瘦A字短裙可拆卸腰带设计皮裙女'
    compareString(str3,'裙',1)
    compareString(str3, '半身裙',0.8)
    compareString(str3, '裙子',0.8)